// pages/order/detail/detail.js
const app = getApp()

Page({
  data: {
    order: null,
    orderStatus: {
      pending: '待接单',
      taken: '已接单',
      processing: '进行中',
      completed: '已完成',
      cancelled: '已取消'
    },
    statusColors: {
      pending: '#ffc107',
      taken: '#17a2b8',
      processing: '#28a745',
      completed: '#28a745',
      cancelled: '#dc3545'
    }
  },

  onLoad(options) {
    this.setData({
      orderId: options.id
    })
    this.loadOrderDetail()
  },

  loadOrderDetail() {
    // 模拟订单详情数据
    const mockOrders = [
      {
        id: 1,
        type: 'take',
        title: '帮忙取快递到宿舍',
        price: 5,
        status: 'pending',
        createTime: '2024-01-15 10:30:25',
        location: '南门快递点',
        destination: '3号楼201宿舍',
        description: '需要帮忙取一个中通快递，包裹不大，重量约1kg左右',
        customer: { 
          name: '张同学', 
          avatar: '/images/avatar1.png',
          phone: '138****1234',
          rating: 4.8
        },
        taker: null,
        timeline: [
          { time: '2024-01-15 10:30:25', action: 'create', desc: '订单创建' }
        ]
      },
      {
        id: 2,
        type: 'take',
        title: '代买午餐',
        price: 8,
        status: 'taken',
        createTime: '2024-01-15 09:15:10',
        location: '食堂一楼麻辣烫窗口',
        destination: '图书馆3楼自习区',
        description: '想要一份麻辣烫，微辣，不要香菜，多加麻酱',
        customer: { 
          name: '李同学', 
          avatar: '/images/avatar2.png',
          phone: '139****5678',
          rating: 4.9
        },
        taker: { 
          name: '我', 
          avatar: app.globalData.userInfo.avatarUrl,
          phone: '136****9012',
          rating: 4.7
        },
        timeline: [
          { time: '2024-01-15 09:15:10', action: 'create', desc: '订单创建' },
          { time: '2024-01-15 09:25:30', action: 'take', desc: '我已接单' }
        ]
      }
    ]

    const order = mockOrders.find(item => item.id == this.data.orderId)
    if (order) {
      this.setData({ order })
    }
  },

  takeOrder() {
    if (this.data.order.status === 'pending') {
      this.updateOrderStatus('taken', '我已接单')
    }
  },

  startService() {
    if (this.data.order.status === 'taken') {
      this.updateOrderStatus('processing', '开始服务')
    }
  },

  completeOrder() {
    if (this.data.order.status === 'processing') {
      this.updateOrderStatus('completed', '订单完成')
    }
  },

  cancelOrder() {
    wx.showModal({
      title: '确认取消',
      content: '确定要取消这个订单吗？',
      success: (res) => {
        if (res.confirm) {
          this.updateOrderStatus('cancelled', '订单取消')
        }
      }
    })
  },

  updateOrderStatus(newStatus, actionDesc) {
    const timelineItem = {
      time: new Date().toLocaleString('zh-CN'),
      action: newStatus,
      desc: actionDesc
    }

    const updatedOrder = {
      ...this.data.order,
      status: newStatus,
      timeline: [...this.data.order.timeline, timelineItem]
    }

    if (newStatus === 'taken') {
      updatedOrder.taker = {
        name: '我',
        avatar: app.globalData.userInfo.avatarUrl,
        phone: '136****9012',
        rating: 4.7
      }
    }

    this.setData({ order: updatedOrder })

    wx.showToast({
      title: '操作成功',
      icon: 'success'
    })
  },

  contactUser() {
    const user = this.data.order.taker || this.data.order.customer
    wx.makePhoneCall({
      phoneNumber: user.phone
    })
  },

  copyAddress() {
    const address = this.data.order.destination
    wx.setClipboardData({
      data: address,
      success: () => {
        wx.showToast({
          title: '地址已复制',
          icon: 'success'
        })
      }
    })
  },

  onShareAppMessage() {
    return {
      title: `校园服务：${this.data.order.title}`,
      path: `/pages/order/detail/detail?id=${this.data.orderId}`
    }
  }
})